package com.example.dnfgamercommunitybackend.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.example.dnfgamercommunitybackend.dao.AccountDao;
import com.example.dnfgamercommunitybackend.entity.Account;
import com.example.dnfgamercommunitybackend.entity.ResponseResult;
import com.example.dnfgamercommunitybackend.service.AccountService;
import com.example.dnfgamercommunitybackend.util.DfaUtil;
import com.example.dnfgamercommunitybackend.util.JwtUtil;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;
@Service
public class AccountServiceImpl implements AccountService {
    @Resource
    private AccountDao accountDao;
    @Resource
    private PasswordEncoder passwordEncoder;
    @Resource
    private JwtUtil jwtUtil;
    @Resource
    private DfaUtil dfaUtil;
    @Override
    public String register(Account account) {
        if(Objects.isNull(account)){
            return JSONObject.toJSONString(ResponseResult.failure("用户信息不能为空"));
        }else {
            //检查用户名是否唯一
            Account dbAccount = accountDao.getAccountByUsername(account.getUsername());
            if (!Objects.isNull(dbAccount)){
                return JSONObject.toJSONString(ResponseResult.failure(503,"用户名已存在"));
            }else {
                //过滤用户名，检查是否合规
                if (dfaUtil.textFilter(account.getUsername())){
                    //检查昵称
                    if (dfaUtil.textFilter(account.getNickname())){
                        //用户名唯一，加密密码
                        String encodePassword = passwordEncoder.encode(account.getPassword());
                        account.setPassword(encodePassword);
                        //向数据库中添加数据
                        int result = accountDao.addAccount(account);
                        if (result > 0){
                            int userId = accountDao.getAccountIdByUsername(account.getUsername());
                            int insertResult = accountDao.addUserAccountRole(userId);
                            if (insertResult>0){
                                return JSONObject.toJSONString(ResponseResult.success("注册成功"));
                            }else{
                                return JSONObject.toJSONString(ResponseResult.failure("注册发生错误"));
                            }
                        }else {
                            return JSONObject.toJSONString(ResponseResult.failure("注册发生错误"));
                        }
                    }
                    return ResponseResult.failureStringResult(505,"昵称不合法，请修改");
                }
                return ResponseResult.failureStringResult(505,"用户名不合法，请修改");
            }
        }

    }

    @Override
    public String updateProfile(Account account,String token) {
        //判断请求体是否为空
        if (Objects.isNull(account)){
            return ResponseResult.failureStringResult("请填写数据");
        }else {
            //过滤昵称，检查合法性
            if(dfaUtil.textFilter(account.getNickname())){
                //获取账户id
                int id = Integer.parseInt(jwtUtil.getUserIdFromToken(token));
                account.setId(id);
                //加密密码
                account.setPassword(passwordEncoder.encode(account.getPassword()));
                //更新数据
                if (accountDao.updateProfile(account) > 0){
                    return ResponseResult.successStringResult("修改成功");
                }else {
                    return ResponseResult.failureStringResult("修改失败");
                }
            }
            return ResponseResult.failureStringResult(505,"昵称不合法，请修改");
        }

    }

    @Override
    public String getProfile(String authorization) {
        //获取用户id
        int userId = Integer.parseInt(jwtUtil.getUserIdFromToken(authorization));
        //查询用户信息
        Account account = accountDao.getProfile(userId);
        if (Objects.isNull(account)){
            return ResponseResult.failureStringResult("获取个人信息失败");
        }
        return ResponseResult.successStringResult("获取个人信息成功",account);
    }
}
